<?php
class pUserAction extends pBaseAction{
    //support search
    public function index(){/*{{{*/
        $conds=array();
        if(array_key_exists('q',$_GET)){
            $key=trim($_GET['q']);
            if($key){
                $conds[]='u.account like \'%'.$key.'%\'';
            }
        }
        $condstr='';
        if(count($conds)>0){
            $condstr=' and ('.implode(' or ',$conds).' )';
        }
        $dao=new UserModel();
        $count=$dao->count("account not in ('system') and status>0".str_replace('u.','',$condstr));
        if($count>0){
            require LIB_PATH.'Util/PageEx.class.php';
            $p=new PageEx($count,15);
            $dbPrefix=C('db_prefix');
            $this->assign('list',$dao->field('u.*,r.name rolename')
                                        ->where("u.account not in ('system') and u.status>0".$condstr)
                                        ->table($dbPrefix.'user u')
                                        ->join($dbPrefix.'role r on r.id=u.role')
                                        ->limit($p->firstRow.','.$p->listRows)
                                        ->findAll()
                );
            $this->assign('page',$p->show());
        }
        $this->display();
    }/*}}}*/

    public function add(){
        $dao=new RoleModel();
        $this->assign('roles',$dao->field('id,name')->where('status=\'1\'')->findAll());
        $this->display();
    }

    public function save(){
        $dao=new UserModel();
        $rs=$dao->addUser();
        if($rs){
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('failure',$dao->getError());
        }
    }

    public function edit(){/*{{{*/
        $id=$this->getFirstSid();
        if(false!==$id){
            $dao=new UserModel();
            $rs=$dao->field('id,account,role,realname,email')->where("id={$id}")->find();
            if($rs){
                $roleDao=new RoleModel();
                $this->assign('roles',$roleDao->field('id,name')->where('status=\'1\'')->findAll());
                $this->assign('rs', $rs);
                return $this->display();
            }
        }
        $this->fail('更改用户失败','没有找到用户信息');
    }/*}}}*/

    public function update(){
        $dao=new UserModel();
        $rs=$dao->updateUser();
        if($rs){
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('failure',$dao->getError());
        }
    }

    /**
     * 删除用户
     *
     * 与用户相关的信息均将被删除
     */
    public function removal(){
        $uids=$this->getSid();
        if($uids===false){
            return $this->ajaxReturn('failure','请先选定要删除的用户');
        }
        $uids=str_replace($_SESSION[C('USER_AUTH_KEY')]['id'],'0',$uids); //不能删除的对象
        $dao=new UserModel();
        $rs=$dao->removalUser($uids);
        if($rs){
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('failure',$dao->getError());
        }
    }

    public function report(){
    }

    public function check(){/*{{{*/
        noCache();
        if(array_key_exists('type', $_GET)){
            $type=$_GET['type'];
            $val=$_GET['val'];
            if($type=='accountUsed'){   //检查帐号是否唯一
                $dao=new UserModel();
                $checkUserRs=$dao->where('account=\''.$val.'\'')->find();
                if(false===empty($checkUserRs)){
                    exit(json_encode(array('ret'=>'fail','msg'=>'用户名&nbsp;'.$val.'&nbsp;已经被使用')));
                }else{
                    exit(json_encode(array('ret'=>'ok','msg'=>'用户名&nbsp;'.$val.'&nbsp;可以使用')));
                }
            }else if($type=='accountUsedByEdit'){
                if($val!=$_GET['origin']){
                    $dao=new UserModel();
                    $checkUserRs=$dao->where('account=\''.$val.'\'')->find();
                    if(false===empty($checkUserRs)){
                        exit(json_encode(array('ret'=>'fail','msg'=>'用户名&nbsp;'.$val.'&nbsp;已经被使用')));
                    }
                }
                exit(json_encode(array('ret'=>'ok','msg'=>'用户名&nbsp;'.$val.'&nbsp;可以使用')));
            }
        }else{
            exit("data losted");
        }
    }/*}}}*/
}
